package com.ly.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.base.ResponseData;
import com.ly.dto.CarInfoDto;
import com.ly.entity.Car;
import com.ly.entity.SysUser;
import com.ly.mapper.CarMapper;
import com.ly.param.CarAuditParam;
import com.ly.param.CarParam;
import com.ly.service.CarService;
import com.ly.service.SysUserService;
import com.ly.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.print.DocFlavor;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    @Autowired
    private SysUserService sysUserService;

    @Value("${car.http}")
    private String url;

    @Override
    public ResponseData addCarInfo(Car car, Principal principal) {

        SysUser sysUser = sysUserService.getByUserName(principal.getName());

        if(sysUser.getSysRole().getCode().equalsIgnoreCase("normal")){
            return ResponseData.fail("请先加入会员才能发布！");
        }

        car.setAuditStatus(0);
        car.setLoanableStatus(0);
        car.setCreatedBy(sysUser.getId());
        car.setCreatedAt(DateUtils.getYyyyMMddHHmmss(new Date()));

        save(car);

        return ResponseData.success("发布成功！");

    }


    @Override
    public ResponseData updateCarInfo(Car car, Principal principal) {

        SysUser sysUser = sysUserService.getByUserName(principal.getName());


        Car car1 = getById(car.getId());
        if(car1.getLoanableStatus()==1||car1.getLoanableStatus()==2){
            return ResponseData.fail("车辆已出租，不能修改！");
        }

        car.setAuditStatus(0);
        car.setUpdatedBy(sysUser.getId());
        car.setUpdatedAt(DateUtils.getYyyyMMddHHmmss(new Date()));
        saveOrUpdate(car);

        return ResponseData.success("修改成功！");
    }

    @Override
    public ResponseData auditCarInfo(CarAuditParam carAuditParam, Principal principal) {

        SysUser sysUser = sysUserService.getByUserName(principal.getName());

        Car car = getById(carAuditParam.getId());
        if(car==null){
            return ResponseData.fail("发布的信息不存在！");
        }

        if(carAuditParam.getAuditRemark()!=null){
            car.setAuditRemark(carAuditParam.getAuditRemark());
        }
        car.setAuditStatus(carAuditParam.getAuditStatus());
        car.setAuditUserId(sysUser.getId());
        car.setAuditTime((DateUtils.getYyyyMMddHHmmss(new Date())));

        updateById(car);
        return ResponseData.success("审核完成");
    }

    @Override
    public Page<CarInfoDto> pageCarInfoList(CarParam carParam) {
        Page<CarInfoDto> page = new Page<>(carParam.getPage(),carParam.getSize());
        Page<CarInfoDto> page1 = baseMapper.adminPageCarList(page,carParam.getName(),carParam.getStatTime(),carParam.getEndTime(),carParam.getKeyValues(),carParam.getAuditStatus());
//        List<String> ids = page1.getRecords().stream().map(CarInfoDto::getId).collect(Collectors.toList());
        page1.getRecords().stream().forEach(e->{
            if(e.getCarUrl()!=null){
                e.setCarUrl(url+e.getCarUrl());
                e.setUrls(Arrays.asList(e.getCarUrl()));
            }
        });
        return page1;
    }

    @Override
    public Page<CarInfoDto> pageCarInfo(CarParam carParam) {
        Page<CarInfoDto> page = new Page<>(carParam.getPage(),carParam.getSize());
        Page<CarInfoDto> page1 = this.baseMapper.ordinaryPageCarList(page,carParam.getName(),carParam.getStatTime(),carParam.getEndTime(),carParam.getKeyValues(),carParam.getLoanableStatus());
        page1.getRecords().stream().forEach(e->{
            if(e.getCarUrl()!=null){
                e.setCarUrl(url+e.getCarUrl());
                e.setUrls(Arrays.asList(e.getCarUrl()));
            }
        });
        return page1;
    }

    @Override
    public Page<CarInfoDto> vipPageCarInfoList(CarParam carParam, Principal principal) {
        SysUser sysUser = sysUserService.getByUserName(principal.getName());
        Page<CarInfoDto> page = new Page<>(carParam.getPage(),carParam.getSize());
        Page<CarInfoDto> page1 = this.baseMapper.vipPageCarList(page,carParam.getName(),carParam.getStatTime(),carParam.getEndTime(),carParam.getKeyValues(),carParam.getLoanableStatus(),carParam.getAuditStatus(),sysUser.getId());
        page1.getRecords().stream().forEach(e->{
            if(e.getCarUrl()!=null){
                e.setCarUrl(url+e.getCarUrl());
                e.setUrls(Arrays.asList(e.getCarUrl()));
            }
        });
        return page1;
    }

    @Override
    public ResponseData deleteCarInfo(String id) {
        Car car = getById(id);
        if(car==null){
            return ResponseData.fail("删除的信息不存在！");
        }
        if(car.getLoanableStatus()==1||car.getLoanableStatus()==2){
            return ResponseData.fail("车辆已出租，不能删除！");
        }
        car.setAuditStatus(0);
        updateById(car);
//        removeById(id);
        return ResponseData.success("撤销成功！");
    }

    @Override
    public Car findByCarId(String id) {
        return getById(id);
    }
}
